package com.student_system.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.student_system.system.entity.SysUser;
import com.student_system.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljy
 * @since 2025-05-14
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;

    /**
     * register(保存用户接口)
     * @param sysUser
     * @return
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody SysUser sysUser) {

        Map<String, Object> map = new HashMap<>();

        sysUserService.save(sysUser);

        map.put("success", "success");
        return map;
    }

    /**
     * login(对比信息)
     * @param sysUser
     * @return
     */
    @GetMapping("/login")
    public String login(@ModelAttribute SysUser sysUser) {
        String flag = "success";
        //根据页面传过来的用户名去数据库查找该用户信息
        SysUser user = sysUserService.findByName(sysUser.getUserName());
        if(user == null){
            flag = "用户不存在";
        }else if(!user.getPassword().equals(sysUser.getPassword())){
            flag = "密码错误";
        }else {
            return "登录成功";
        }
        return flag;
    }

    //增加
    @PostMapping("/addUser")
    public String addUser(@RequestBody SysUser sysUser){
        /**相当于insert into sys_role (role_name,description) value(?,?)*/
        sysUserService.save(sysUser);
        return "success";
    }

    //删除
    @DeleteMapping("/delUser/{userId}")
    public String delUser(@PathVariable Integer userId){
        /** 相当于delete from sys_role  where role_id =? */

        sysUserService.removeById(userId);
        return "success";
    }

    //修改
    @PutMapping("/updateUser")
    public String updateRole(@RequestBody SysUser sysUser){
        /** 相当于update sys_role set role_name =?, description =? where role id =?  */
        sysUserService.updateById(sysUser);
        return "success";
    }

    /**
     * 根据条件查询用户信息
     * @param userId 可选，用户ID
     * @param userName 可选，用户名称
     * @return 角色列表或单个角色
     */
    @GetMapping("/searchUser")
    public Object searchUser(
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) String userName) {

        if (userId != null) {
            // 优先按ID精确查询
            return sysUserService.getById(userId);
        }

        // 按名称模糊查询
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (userName != null) {
            queryWrapper.like("user_name", userName);
        }

        List<SysUser> users = sysUserService.list(queryWrapper);

        // 如果只查询到一条记录，直接返回该对象
        if (users.size() == 1) {
            return users.get(0);
        }

        return users;
    }

    //分页查询
    @GetMapping("/pageUser")
    public Page<SysUser> pageRole(@RequestParam(required = false) String userName,
                                  @RequestParam(required = false) Integer current,
                                  @RequestParam(required = false) Integer size){

        if(current == null){
            current = 1;
        }
        if (size == null){
            size = 10;
        }
        //创建分页对象，将当前页和每页显示的数量传进去(因为mybatiesPlus的分页插件只支持Page对象)
        Page<SysUser> page = new Page<>(current,size);
        // 调用service方法，查询角色列表，并将分页对象和查询条件传进去
        Page<SysUser> sysUserPage = sysUserService.queryUserPage(page,userName);

        return sysUserPage;
    }
}
